# encoding: utf-8
"""
交互组件包
"""
import uuid
from typing import Literal, Any, Optional, ClassVar
from pydantic import BaseModel, Field
import time

from .base import PlanSchema, Plan, Action

'''
创建计划
'''


class InteractionBase(BaseModel):
    session_id: str = Field(description='回话ID')
    event_id: str = Field(default_factory=lambda: str(uuid.uuid1()), description='事件ID')
    eventTime: str = Field(default_factory=lambda: time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), description='事件发生的时间')

class Heartbeat(InteractionBase):
    name: str = 'Heartbeat'
    message: Optional[str]


class PlanScheduled(InteractionBase):
    name: str = 'PlanScheduled'
    planSchema: PlanSchema = Field(description='整体的计划表')


'''
变更计划
'''


class PlanChanged(InteractionBase):
    name: str = 'PlanChanged'
    domain: Literal['plan', 'action'] = Field(description='变动的范围')
    method: Literal['delete', 'add'] = Field(description='变动的方式')
    data: Any = Field(description='变动的数据')


class DeletePlan(PlanChanged):
    domain: str = 'plan'
    method: str = 'delete'
    data: dict = Field(description='需要删除的计划ID')


class AddPlan(PlanChanged):
    domain: str = 'plan'
    method: str = 'add'
    data: list[Plan] = Field(description='需要添加的计划')


class DeleteAction(PlanChanged):
    domain: str = 'action'
    method: str = 'delete'
    data: dict = Field(description='需要删除的动作ID')


class AddAction(PlanChanged):
    domain: str = 'action'
    method: str = 'add'
    data: list[Action] = Field(description='需要填的Action')

'''
plan操作
'''


class TaskBegin(InteractionBase):
    name: str = 'TaskBegin'
    id: str = Field(description='任务ID')
    data: Plan = Field(description='任务的详细数据')


class TaskEnd(InteractionBase):
    name: str = 'TaskEnd'
    id: str = Field(description='任务ID')
    data: Plan = Field(description='任务的详细数据')


'''
执行动作
'''


class ActionBegin(InteractionBase):
    name: str = 'ActionBegin'
    id: str = Field(description='action id')
    data: Action = Field(description='action的详细数据')


class ActionEnd(InteractionBase):
    name: str = 'ActionEnd'
    id: str = Field(description='action id')
    data: Action = Field(description='action的详细数据')


class Finished(InteractionBase):
    name: str = 'Finished'
    status: Literal['completed', 'failed'] = Field(description='是否成功')
    result: Any = Field(default=None, description='结果')


class StreamResult(BaseModel):
    id: str = Field(description='替换的ID')
    content: str = Field(description='值')


class StreamReport(InteractionBase):
    name: str = 'StreamReport'
    structure: str = Field(description='报告结构')
    result: StreamResult = Field(description='流式输出的结果')


# 定义的传入的模板结构
class ReportTemplateContent(BaseModel):
    level: int = Field(description='标题层级')
    title: str = Field(description='标题')
    active: bool = Field(description='')
    prompt: Optional[str] = Field(default='', description='内容要求')


class SupplyReportTemplate(BaseModel):
    name: str = Field(description='模板名称')
    fullPrompt: Optional[str] = Field(default='', description='全文模板')
    remark: Optional[str] = Field(default='', description='模板的描述')
    content: list[ReportTemplateContent] = Field(default=[], description='文章的内容')


